package com.example.hotel_manage.service.admin;

/**
 * 【管理员】增删改查
 */
@Service
public class AdminService {

    @Autowired
    private AdminDAO adminDAO;


    /**
     * 新增【管理员】
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public AdminPO save(AdminAddDTO adminDTO) {
        AdminPO admin = AdminMapper.INSTANCE.fromAddDTO(adminDTO);
        adminDAO.save(admin);
        return admin;
    }

    /**
     * 修改【管理员】
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public AdminPO update(AdminUpdateDTO adminUpdateDTO) {
        Integer id = adminUpdateDTO.getId();
        AdminPO admin = this.getAdmin(id, true);
        AdminMapper.INSTANCE.setUpdateDTO(admin, adminUpdateDTO);
        adminDAO.update(admin);
        return admin;
    }

    /**
     * 查询分页列表
     */
    public PageVO<AdminListVO> list(AdminQO adminQO) {
        PageVO<AdminListVO> page = adminDAO.findByPage(adminQO);
        return page;
    }

    /**
     * 根据主键获取【管理员】
     */
    public AdminPO getAdmin(Integer id, boolean force) {
        AdminPO admin = adminDAO.findById(id);
        if (force && admin == null) {
            throw new BusinessException(ErrorCode.RECORD_NOT_FIND);
        }
        return admin;
    }


    /**
     * 查询【管理员】详情
     */
    public AdminShowVO show(Integer id) {
        AdminPO admin = this.getAdmin(id, true);
        AdminShowVO showVO = AdminMapper.INSTANCE.toShowVO(admin);
        return showVO;
    }

    /**
     * 删除【管理员】
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public int delete(Integer... ids) {
        int count = 0;
        for (Integer id : ids) {
            count += adminDAO.delete(id);
        }
        return count;
    }


}


